#!/bin/bash

cd "$(git rev-parse --show-toplevel)"
DISALLOWED_PATTERN='KJ_DBG'

if git diff-index -G"$DISALLOWED_PATTERN" --cached HEAD --diff-filter=AM -U0 | grep -i --color -E "$DISALLOWED_PATTERN"
then
  echo -e "\nERROR, KJ_DBG is not allowed in checked-in source code:"
  echo -e "  see https://github.com/capnproto/capnproto/blob/v2/c%2B%2B/src/kj/debug.h#L42\n"
  echo -e "To commit anyway, use --no-verify\n"
  exit 1
fi

clang_format_check() {
  source "$(dirname -- $BASH_SOURCE)/../tools/unix/find-python3.sh"
  PYTHON_PATH=$(get_python3)
  if [[ -z "$PYTHON_PATH" ]]; then
    echo
    echo "python3 is required for formatting and was not found"
    echo
    echo "ERROR: you must either install python3 and try pushing again or run `git push` with `--no-verify`"
    return 1
  fi

  set +e
  $PYTHON_PATH "$(dirname -- $BASH_SOURCE)/../tools/cross/format.py" --check git --staged
  EXIT_CODE=$?
  set -e
  case $EXIT_CODE in
    0)
      # No lint.
      return 0
      ;;
    1)
      echo
      echo "ERROR: changes staged for commit have lint. Pass '--no-verify' or '-n' to skip."
      echo
      echo "To fix lint:"
      echo "  just format"
      echo
      return 1
      ;;
    2)
      echo
      echo "ERROR: failed to run format.py, Pass '--no-verify' or '-n' to skip."
      echo
      return 1
      ;;
  esac
}

clang_format_check
